package chapter1.fundamentals.exercise;

import edu.princeton.cs.algs4.StdRandom;

/**
 * @author quanlinlin on 2018/10/25 21:40.
 * @version 1.0
 */
public class E_13 {

    public static void main(String[] args) {
        int[][] array = createArray();
        printaArray(array);
        System.out.println("-------------");
        printaArray(arrayTranspose(array));
    }

    private static void printaArray(int[][] array) {
        for (int i = 0; i < array.length; i++) {
            for (int j = 0; j < array[i].length; j++) {
                System.out.print(array[i][j] + " ");
            }
            System.out.println();
        }
    }

    private static int[][] createArray() {
        int length = StdRandom.uniform(3, 10);
        int height = StdRandom.uniform(3, 10);
        int a[][] = new int[height][length];
        for (int i = 0; i < height; i++) {
            for (int j = 0; j < length; j++) {
                a[i][j] = StdRandom.uniform(10);
            }
        }
        System.out.println("length:"+length+"\theight:"+height);
        return a;
    }


    public static int[][] arrayTranspose(int[][] array) {
        int[][] newArray = new int[array[0].length][array.length];
        for (int i = 0; i < array.length; i++) {
            for (int j = 0; j < array[i].length; j++) {
                newArray[j][i] = array[i][j];
            }
        }
        return newArray;
    }
}
